<?php

namespace App\Http\Controllers\Frontend;


use App\Logics\SiteLogic;

class TagsController extends BaseController
{
    // 标签 初始化
    public function initialize()
    {
        parent::initialize();
    }

    // 首页
    public function index()
    {
        // 标签信息
        $option = $this->optLogic();
        $info   = $this->getTagInfo();
        if(empty($info)){
            return redirect('/');
        }

        // 传值
        $option->assign('tagId', $info['id']);
        $option->assign('cateId', 0);
        $option->assign('info', $info);
        $option->assign('nextPage', $this->nextPage('tags', $info));

        return $this->dispaly('frontend.tags');
    }


    // 加载更多
    public function more()
    {
        $info  = $this->getTagInfo();
        $tagId = $info['id'] ?? 0;


        $component = new Components([]);
        $html      = $component->tag($tagId);
        $nextPage  = $this->nextPage('tags', $info);


        return $this->success(['html' => (string)$html, 'nextUrl' => $nextPage]);
    }

    // 获取标签信息
    protected function getTagInfo()
    {
        $option = $this->optLogic();
        $title  = $option->getData('title', null, 'trim');

        // 标签
        $title = str_replace('-', ' ', $title);
        $info  = (new SiteLogic())->getTag($title);

        return $info;
    }
}


